package com.zzu.model.impl;

import android.content.Context;
import android.util.Log;

import com.zzu.model.bean.Delivery;
import com.zzu.model.bean.PackageItem;
import com.zzu.model.model.PackageModel;
import com.zzu.model.bean.DLS;
import com.zzu.model.service.PackageService;
import com.zzu.model.service.RegionService;
import com.zzu.presenter.PackageQueryListener;
import com.zzu.utils.ServiceCreator;
import com.zzu.utils.ToastAndDialogUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-08 14:48
 */
public class PackageModelImpl implements PackageModel {

    Context context = null;
    public PackageModelImpl(Context context){
        this.context = context;
    }

    PackageService packageService = ServiceCreator.getRetrofit(PackageService.class);
    RegionService regionService = ServiceCreator.getRetrofit(RegionService.class);

    private final String TAG = UserModelImpl.class.getName();

    @Override
    public void getDLSTask(String twn, PackageQueryListener<List<DLS>> packageQueryListener) {

        //通过区名返回地区代码，然后获得该地区需要揽收的包裹
        Call<String> call = regionService.getRegionCodeByTwn(twn);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {

                //获得相应得到的地区代码
                String code = response.body();
                if(code == null || "".equals(code)){
                    //没有获取到改地址的信息
                    packageQueryListener.getNoDate();
                }else{
                    //通过地区代码获取需要揽收的包裹
                    Call<List<DLS>> dlsTask = packageService.getDLSTask(code);
                    dlsTask.enqueue(new Callback<List<DLS>>() {
                        @Override
                        public void onResponse(Call<List<DLS>> call, Response<List<DLS>> response) {
                            List<DLS> lists = response.body();
                            if(lists == null || lists.size() == 0){
                                //没有数据
                                packageQueryListener.getNoDate();
                            }else{
                                //获取到了数据进行回调
                                packageQueryListener.getDates(lists);
                            }
                        }
                        @Override
                        public void onFailure(Call<List<DLS>> call, Throwable t) {
                            Log.i(TAG,"请求服务器失败"+t);
                            ToastAndDialogUtils.showShort(context,"请求服务器失败"+t);
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                ToastAndDialogUtils.showShort(context,"请求服务器失败"+t);
                Log.i(TAG,"请求服务器失败"+t+call);
            }

        });

    }

    @Override
    public void getDeliveryTaskByHandler(String handler, PackageQueryListener<List<Delivery>> packageQueryListener) {

        //通过派送人id查询派送任务列表
        Call<List<Delivery>> call = packageService.getDeliveryTaskByHandler(handler);
        call.enqueue(new Callback<List<Delivery>>() {
            @Override
            public void onResponse(Call<List<Delivery>> call, Response<List<Delivery>> response) {
                List<Delivery> lists = response.body();
                if(lists == null || lists.size() == 0){
                    //没有数据
                    packageQueryListener.getNoDate();
                }else{
                    packageQueryListener.getDates(lists);
                }
            }

            @Override
            public void onFailure(Call<List<Delivery>> call, Throwable t) {
                Log.i(TAG,"请求服务器失败"+t);
                ToastAndDialogUtils.showShort(context,"请求服务器失败"+t);
            }
        });
    }

    @Override
    public void getPackageById(String id, PackageQueryListener<PackageItem> packageQueryListener) {

        Call<PackageItem> call = packageService.getPackageById(id);
        call.enqueue(new Callback<PackageItem>() {
            @Override
            public void onResponse(Call<PackageItem> call, Response<PackageItem> response) {
                packageQueryListener.getDates(response.body());

            }

            @Override
            public void onFailure(Call<PackageItem> call, Throwable t) {
                Log.i(TAG,"请求服务器失败"+t);
                ToastAndDialogUtils.showShort(context,"请求服务器失败"+t);
            }
        });

    }
}
